<template>
  <div>
    <div class="box">
      <el-button type="primary" @click="handleAddCarousel">增加活动</el-button>
    </div>
    <div class="box">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="carouselId" label="ID" width="80">
        </el-table-column>
        <el-table-column prop="carouselName" label="轮播名称" width="320">
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间">
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleRename(scope.row)">修改</el-button>
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑图片</el-button>
            <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div style="margin: 10px;">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="pageNum"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        >
      </el-pagination>
    </div>

    <!-- 轮播对话框 -->
    <el-dialog :title="formTitle" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="轮播名称" :label-width="formLabelWidth" prop="carouselName">
          <el-input v-model="form.carouselName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属栏目" :label-width="formLabelWidth" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择">
              <el-option
                v-for="item in articleTypes"
                :key="item.typeId"
                :label="item.typeName"
                :value="''+item.typeId">
              </el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCarousel">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    bannerAddIndex,
    bannerSetIndex,
    bannerDelIndex,
    bannerGetIndex,
    bannerListIndex,
    bannerAddBody,
    bannerSetBody,
    bannerDelBody,
    bannerGetBody,
    bannerListBody,
    articleTypeListAll
  } from '@/api/news.js'
  export default {
    name: 'Carousel',
    data() {
      return {
        tableData: [],
        total: 0,
        pageSize: 10,
        pageNum: 1,
        formTitle: '增加轮播',
        dialogFormVisible: false,
        formLabelWidth: '120px',
        articleTypes: [],
        form: {
          carouselId: 0,
          carouselName: '',
          typeId: null,
        },
        rules: {
          carouselName: [
            { required: true, message: '请输入轮播名称', trigger: 'change' }
          ],
        },
      }
    },
    mounted() {
      this.loadList()
      this.loadTypeList()
    },
    methods: {
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.pageNum = val
        this.loadList()
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        this.loadList()
      },
      loadList() {
        let param = {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
        bannerListIndex(param).then(res => {
          console.log('getCarouselByPage', res);
          if (res.code == 200) {
            this.tableData = res.data.records
            this.pageNum = res.data.current
            this.pageSize = res.data.size
            this.total = res.data.total
          }
        })
      },
      loadTypeList() {
        articleTypeListAll().then(res => {
          console.log('loadList', res)
          if(res.code == 200) {
            this.articleTypes = [{typeId:0,typeName:'首页'}, ...res.data]
          }
        })
      },
      handleAddCarousel() {
        this.formTitle = '增加轮播'
        this.form.carouselId = 0
        this.form.carouselName = ''
        this.dialogFormVisible = true
      },
      submitCarousel() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let param = this.form
            if(this.form.carouselId == 0) {
              bannerAddIndex(param).then(res => {
                if (res.code == 200) {
                  this.$message.success('添加成功')
                  this.dialogFormVisible = false
                  this.loadList()
                }
              })
            }
            else {
              bannerSetIndex(param).then(res => {
                if (res.code == 200) {
                  this.$message.success('修改成功')
                  this.dialogFormVisible = false
                  this.loadList()
                }
              })
            }


          } else {
            this.$message.error('请把表单数据填写完整')
            return false;
          }
        });
      },
      handleRename(row) {
        console.log('handleRename', row);
        this.formTitle = '修改轮播'
        this.form.carouselId = row.carouselId
        this.form.carouselName = row.carouselName
        this.form.typeId = ''+row.typeId
        this.dialogFormVisible = true
      },
      handleEdit(row) {
        this.$router.push('/news/carouselAdd/'+row.carouselId)
      },
      handleDelete(row) {

      }
    }
  }
</script>

<style>
  .box {
    margin: 10px;
  }
</style>
